package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;

import com.example.myapplication.databinding.ActivityVideoBinding;

import java.util.ArrayList;
import java.util.List;

public class VideoActivity extends AppCompatActivity {
    private ActivityVideoBinding videoBinding;
    List<Fragment> fragmentList = new ArrayList<>();
    VideoViewPagerAdapter pagerAdapter;
    List<NewCarListModel.ChildrenBeanX> replayBean;

    //先定义
    private static final int REQUEST_EXTERNAL_STORAGE = 1;

    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        videoBinding = DataBindingUtil.setContentView(this, R.layout.activity_video);
        verifyStoragePermissions(this);

        replayBean = new ArrayList<>();
        List<String> imeiList = new ArrayList<>();
        for (int i = 1; i < 4; i++) {
            fragmentList.add(VideoFragment.newInstance("863015805184543", i));
        }
        pagerAdapter = new VideoViewPagerAdapter(getSupportFragmentManager(), fragmentList);
        videoBinding.viewPager.setAdapter(pagerAdapter);
        videoBinding.viewPager.setCurrentItem(0);
    }

    //然后通过一个函数来申请
    public static void verifyStoragePermissions(Activity activity) {
        try {
            //检测是否有写的权限
            int permission = ActivityCompat.checkSelfPermission(activity,
                    "android.permission.WRITE_EXTERNAL_STORAGE");
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // 没有写的权限，去申请写的权限，会弹出对话框
                ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Intent createIntent(Context context) {
        return new Intent(context, VideoActivity.class);
    }

//    private class HomePagerAdapter extends FragmentPagerAdapter {
//        public HomePagerAdapter(FragmentManager fm) {
//            super(fm);
//        }
//
//        @Override
//        public Fragment getItem(int i) {
//            return fragmentList.get(i);
//        }
//
//        @Override
//        public int getCount() {
//            return fragmentList.size();
//        }
//    }
}
